<template>
  <div class="app-content-wrap" :style="'minHeight:' + gHeight + 'px;'">
    <div class="g-content-title">
      居民管理
    </div>
    <!-- 搜索框 -->
    <el-form :model="userFilter" label-width="80px" size="small">
      <el-row :gutter="15">
        <el-col :span="4">
          <el-form-item label="姓名">
            <el-input v-model="userFilter.userName" placeholder="姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="电话">
            <el-input v-model="userFilter.mobile" placeholder="电话"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label="属性">
            <el-select v-model="userFilter.userAttribute" placeholder="选择属性">
              <el-option v-for="item in subTypeArr" :key="item.value" :value="item.value" :label="item.name">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item>
            <el-button type="primary" size="small" icon="el-icon-search" @click="getUserFromFilter">搜 索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 列表 -->
    <div style="margin:10px 30px;">
      <el-table :data="userListTable" border style="width: 100%">
        <el-table-column prop="name" label="姓名" min-width="100" align="center"></el-table-column>
        <el-table-column prop="gender" label="性别" min-width="120" align="center"></el-table-column>
        <el-table-column prop="mobile" label="联系电话" min-width="140" align="center"></el-table-column>
        <el-table-column prop="cardNo" label="证件号码" min-width="140" align="center"></el-table-column>
        <el-table-column prop="buildNo" label="楼栋号" min-width="140" align="center"></el-table-column>
        <el-table-column prop="buildUnit" label="单元号" min-width="140" align="center"></el-table-column>
        <el-table-column prop="roomNo" label="房间号" min-width="140" align="center"></el-table-column>
        <el-table-column prop="userType" label="人员类型" min-width="140" align="center" :formatter="userTypeToText"></el-table-column>
        <el-table-column prop="userAttribute" label="属性" min-width="240" align="center" :formatter="residentAttributeToText"></el-table-column>
        <el-table-column label="操作" width="190" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="userDetail(scope.row)">详情</el-button>
            <el-button type="text" @click="userDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination background @current-change="handleCurrentChange" :current-page.sync="pagination.currentPage" :page-size="10" layout="total, prev, pager, next,jumper" :total="pagination.total" class="el-page-body"></el-pagination>
    </div>
  </div>
</template>
<script>
import AreaSelect from '../../components/common/select/area.vue'

export default {
  name: 'userInfo',
  components: {
    AreaSelect
  },
  data () {
    return {
      gHeight: this.$store.state.gHeight,
      subTypeArr: [],
      userFilter: {
        userName: '', // 姓名
        mobile: '', // 电话
        deptId: '', // 部门
        userAttribute: ''
      },
      areaIds: {
        areaId1: '', // 所属1级
        areaId2: '', // 所属2级
        areaId3: '' // 所属3级
      },
      userListTable: [],
      pagination: {
        currentPage: 1,
        total: null
      }
    }
  },
  created () {
  },
  methods: {
    userDetail: function (row) {
      this.$router.push({
        name: 'residentDetail',
        params: {
          userId: row.id
        }
      })
    },
    userDelete (row) {
      const id = row.id
      this.$confirm('删除该居民', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = this.$base.getUrl('bizresidentinfoDelete')
        let params = {
          isJson: true,
          url: url,
          data: {
            id: id
          }
        }
        this.$store.dispatch('postForm', params).then((res) => {
          if (res.code === 0) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getUser()
          }
        })
      }).catch(() => {
        this.$message({
          message: '已取消删除',
          type: 'info'
        })
      })
    },
    getUserFromFilter: function () { /// /表单
      this.pagination.currentPage = 1
      this.getUser()
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
      this.getUser()
    },
    getUser () {
      const url = this.$base.getUrl('bizresidentinfoList')
      let params = {
        isJson: true,
        url: url,
        data: {
          name: this.userFilter.userName, // 姓名
          mobile: this.userFilter.mobile, // 电话
          userAttribute: this.userFilter.userAttribute,
          // proveState: this.userFilter.proveState, // 是否认证0:否 1：是
          page: this.pagination.currentPage,
          limit: 10
        }
      }
      this.$store.dispatch('postForm', params).then((res) => {
        let { result } = res
        this.userListTable = result.list
        this.pagination.currentPage = result.currPage
        this.pagination.total = result.totalCount
      })
    },
    // role组件change时
    onRoleChange (val) {
      this.userFilter.role = val
    },
    // 所属变化时
    onAreaChange (val) {
      this.userFilter.deptId = val
    },
    // 居民属性
    residentAttributeToText (row) {
      return this.$base.getNameByConst('select', 'userAttribute', row.userAttribute)
    },
    // 人员类型
    userTypeToText (row) {
      return this.$base.getNameByConst('select', 'userType', row.userType)
    }
  },
  mounted () {
    this.subTypeArr = this.$base.getConst('select', 'userAttribute')
    this.getUser()
  }
}
</script>
<style scoped>
.el-table {
  width: auto !important;
  text-align: center;
  margin: 10px;
}

.el-table >>> th {
  text-align: center;
}
</style>
